<?php
namespace Home\Controller;
use Think\Controller;
/*
 ×品牌管理控制器
 ×
 */
class BrandController extends Controller {
	/*
	 ×品牌管理首页
	 */
	public function index(){
		$page=I('get.page','1','strip_tags');
        $count=M('brand')->where('isdel=0')->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
		$result=M('brand')->where('isdel=0')->limit($pagearr['start'],$pagearr['pagesize'])->select();
		$this->assign('brands',$result);
		$this->assign('pagearr',$pagearr);
		$this->display('index');
	}
	/*
	 ×添加品牌
	 *uid  uid
	 */
	public function addBrand(){
		//检测用户的所在的组只有总部可以添加品牌
		$this->display('addbrand');
	}
	public function saveBrand(){
		$brand=M('brand');
		$brand->create();
		$brand->createtime=time();
		$brand->brandname=I('post.brandname','','strip_tags');
		$brandname=I('post.brandname','','strip_tags');
		$brandid=M('brand')->where("brandname='%s'",$brandname)->getField("brandid");
		if(is_null($brandid)){
		$result=$brand->add();
		if(!empty($result)){
           echo "<script type='text/javascript'>alert('添加品牌成功'); window.location.href='index';</script>";
	 	}else{
	 		echo "<script type='text/javascript'>alert('添加品牌成功'); window.history.go(-1);</script>";
	 	}
	 }else{
	 	   echo "<script type='text/javascript'>alert('该品牌已经存在，添加失败'); window.location.href='index';</script>";

	 }
	}
	/*
	 *编辑品牌
	 *@param brandid 
	 */
	
	public function editBrand(){
		$where['brandid']=$_GET['brandid'];
		if($where['brandid']){
			$brand=M('brand');
			$brands=$brand->where($where)->select();
			$this->assign('brand',$brands[0]);
			$this->assign('brandid',$where['brandid']);
			$this->display('editBrand');
		}else{
			echo "<script type='text/javascript'>alert('参数错误'); window.history.go(-1);</script>";
		}

	}
	/*
	 * 更新品牌
	 * @param  brandid
	 * return bool
	 */
	public function updateBrand()
	{
		$where['brandid']=I('post.brandid','','strip_tags');
		$data['brandname']=I('post.brandname','','strip_tags');
		$brandid=M('brand')->where("brandname='%s'",$data['brandname'])->getField("brandid");
		if(is_null($brandid)){
		$result=M('brand')->where($where)->save($data);
		if($result===false){
			echo "<script type='text/javascript'>alert('修改品牌失败，请重新修改'); window.history.go(-1);</script>";
		}else{
			echo "<script type='text/javascript'>alert('修改品牌成功'); window.location.href='index';</script>";
		}
	}else{
		echo "<script type='text/javascript'>alert('品牌已经存在,添加失败'); window.history.go(-1);</script>";

	}

	}
	/*
	 * ajax 删除品牌
	 * @param brandid
	 * return json
	 */
	public function ajaxDel(){
		$where['brandid']=I('post.brandid','','strip_tags');
		$brand=M('brand');
		$result=$brand->where($where)->delete();
		if(!empty($result)){
			//删除成功
			echo 1;
		}else{
			echo 0;
		}

	}
	/*
	 *搜索品牌
	 *keywords 品牌关键词
	 *return json
	 */
	public function searBrand(){
		
		$arg=array();
		$arg['keywords']=I('post.keywords','','strip_tags');
		
		$brand=D('brand');
		$brands=$brand->index($arg);
		$result=array();

		$result['status']=!empty($brands)?1:0;

		$result['brands']=$brands;

		echo json_encode($result);
		return '';
		


	}
}